<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->

                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal1" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteInfo()" title="删除"><i class="fa fa-trash-o"></i> 删除</button>

                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  type="text" id="name2">
									<input type="button" value="查询" onclick="typetemplateList()" >
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联规格</th>
										  <th class="sorting">关联品牌</th>
									      <th class="sorting">扩展属性</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="typetemplateList">
										<!--查询-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->


                        </div>
                        <!-- 数据表格 /-->




                     </div>
                    <!-- /.box-body -->



  <!-- 增加窗口 -->
  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <td><input  class="form-control" name="name" id="name" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="spec" style="width: 100% " multiple>
								  <option th:each="spec : ${tbSpecificationList}" th:value="${spec.id}">[[${spec.specName}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brand" style="width: 100% " multiple>
								  <option th:each="brand : ${brandList}" th:value="${brand.id}">[[${brand.name}]]</option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table id="kuozhan" class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="insert()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
<!-- 编辑窗口 -->
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <input type="hidden" id="id">
						  <td><input  class="form-control" name="name1" id="name1" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="spec1" style="width: 100% " multiple>
								  <option th:each="spec : ${tbSpecificationList}" th:value="${spec.id}">[[${spec.specName}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brand1" style="width: 100% " multiple>
								  <option th:each="brand : ${brandList}" th:value="${brand.id}">[[${brand.name}]]</option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table id="kuozhan1" class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="uodateBytext">

								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="updateInfo()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
  <a href="javascript:toPage(3)">首页</a>
  <a href="javascript:toPage(1)">上一页</a>
  <a href="javascript:toPage(2)">下一页</a>
  <a href="javascript:toPage(4)">尾页</a>
  第<input type="text" id="pageNum" name="pageNum" size="2px" >页
  共<input type="text" id="total"  name="total" size="2px" readonly="readonly">条
  总<input type="text" id="pages"  name="pages" size="2px" readonly="readonly">页
  每页<input type="text" id="pageSize"  name="pageSize" size="2px" readonly="readonly">条
<script>
    $(function(){
        typetemplateList();
        $("#brand").select2();
        $("#spec").select2();
		$("#brand1").select2();
		$("#spec1").select2();
    })
    function typetemplateList(){
        $.ajax({
            url:"/typetemplate/typetemplateList",
            data:{pageNum:$("#pageNum").val(),pageSize:$("#pageSize").val(),name:$("#name2").val()},
            dataType:"json",
            type:"post",
            async:false,
            success:function(result){
				console.log(result);
				if (result.code == 10000){
				var list = result.data.rows;
				var str ='';
				for (var i= 0;i<list.length;i++){
					str+='<tr>'
					str+='<td><input  type="checkbox" name="box" value="'+list[i].id+'"></td>'
					str+='<td>'+list[i].id+'</td>'
					str+='<td>'+list[i].name+'</td>'

					var specIds = JSON.parse(list[i].specIds);
					str+='<td>'
					var str1 ='';
					for (var o = 0;o<specIds.length;o++){
						str1+=','+specIds[o].text
					}
					str1=str1.substr(1);
					str+=str1;
					str+='</td>'

					var brandIds = JSON.parse(list[i].brandIds)
					str+='<td>'
					var str2 ='';
					for (var u = 0;u<brandIds.length;u++){
						str2+=','+brandIds[u].text;
					}
					str2=str2.substr(1);
					str+=str2;
					str+='</td>'

					var customAttributeItems = JSON.parse(list[i].customAttributeItems)
					str+='<td>'
					var str3 ='';
					for (var u = 0;u<customAttributeItems.length;u++){
						str3+=','+customAttributeItems[u].text;
					}
					str3=str3.substr(1);
					str+=str3;
					str+='</td>'

					str+='<td class="text-center">'
					str+='<button type="button" class="btn bg-olive btn-xs" onclick="selectById('+list[i].id+')" data-toggle="modal" data-target="#editModal" >修改</button>'
					str+='</td>'
					str+='</tr>'
				}
					$("#typetemplateList").html(str);
					pageinstall(result.data);
				}
            },error:function(result){
                alert("系统异常");
            }
        })
    }

	function pageinstall(page){
		$("#pageNum").val(page.pageNum);
		$("#pageSize").val(page.pageSize);
		$("#total").val(page.total);
		$("#pages").val(page.pages);
	}

	function toPage(flag){
		var pageNum = parseInt($("#pageNum").val());
		var pages = parseInt($("#pages").val());
		if (flag == 1){//上一页
			if (pageNum <= 1){
				alert("已经是首页");
				return;
			}
			$("#pageNum").val(pageNum-1);
		}
		else if (flag == 2){//下一页
			if (pageNum >= pages){
				alert("已经是最后一页");
				return;
			}
			$("#pageNum").val(pageNum+1);
		}else if(flag == 3){
			$("#pageNum").val(1);
		}else  if (flag == 4){
			$("#pageNum").val(pages);
		}
		typetemplateList();
	}

	function insert(){
		var brands = $("#brand").select2("data")
		var brandList =[];
		for (var i = 0;i<brands.length;i++){
			brandList.push({id: brands[i].id, text: brands[i].text});
		}
		var brandListJson = JSON.stringify(brandList);

		var specs = $("#spec").select2("data")
		var specList =[];
		for (var i = 0;i<specs.length;i++){
			specList.push({id: specs[i].id, text: specs[i].text});
		}
		var specListJson = JSON.stringify(specList);

		var trs = $("#kuozhan").find("tr")
		var kuozhan =[];
		for (var i = 1;i<trs.length;i++){
			var tds = $(trs[i]).find("td");
			var text = $(tds[1]).find("input").val()
			kuozhan.push({text:text})
		}
		var kuozhanListJson = JSON.stringify(kuozhan);

		$.ajax({
			url: "/typetemplate/toAdd",
			data: {
				name: $("#name").val(),
				brandListJson: brandListJson,
				specListJson: specListJson,
				kuozhanListJson: kuozhanListJson
			},
			type: "post",
			dataType: "json",
			async: false,
			success: function (result) {
				if (result.code == 10000){
					typetemplateList();
				}
			}, error: function (result) {
				alert("系统异常");
			}
		})
	}
	function selectById(id){
		$.ajax({
			url:"/typetemplate/selectById",
			data:{id:id},
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 10000){

					$("#id").val(result.data.id)
					$("#name1").val(result.data.name)

					var brandJson = result.data.brandIds;
					var brandList = JSON.parse(brandJson);
					var ids = [];
					for (var i = 0;i<brandList.length;i++){
						ids.push(brandList[i].id)
					}
					$("#brand1").val(ids).trigger('change');

					var specJson = result.data.specIds;
					var specList = JSON.parse(specJson);
					var ids1 = [];
					for (var i = 0;i<specList.length;i++){
						ids1.push(specList[i].id)
					}
					$("#spec1").val(ids1).trigger('change');

					var str='';
					var cust=JSON.parse(result.data.customAttributeItems)
					for (var i=0;i<cust.length;i++){
						str+='<tr>'
						str+='<td><input type="checkbox" className="icheckbox_square-blue"></td>'
						str+='<td><input type="text" className = "form-control" placeholder ="属性名称"value="'+cust[i].text+'" name="customAttributeItems"></td>'
						str+='<td>'
						str+='<button type="button" className="btn btn-default" title="删除"><i className="fa fa-trash-o"></i> 删除 </button>'
						str+='</td>'
						str+='</tr>'
					}
					$("#uodateBytext").html(str);
				}
			},error:function (result){
				alert("系统错误");
			}
		})
	}
	function updateInfo(){
		var id = $("#id").val();

		var brands = $("#brand1").select2("data")
		var brandList =[];
		for (var i = 0;i<brands.length;i++){
			brandList.push({id: brands[i].id, text: brands[i].text});
		}
		var brandListJson = JSON.stringify(brandList);

		var specs = $("#spec1").select2("data")
		var specList =[];
		for (var i = 0;i<specs.length;i++){
			specList.push({id: specs[i].id, text: specs[i].text});
		}
		var specListJson = JSON.stringify(specList);

		var trs = $("#kuozhan1").find("tr")
		var kuozhan =[];
		for (var i = 1;i<trs.length;i++){
			var tds = $(trs[i]).find("td");
			var text = $(tds[1]).find("input").val()
			kuozhan.push({text:text})
		}
		var kuozhanListJson = JSON.stringify(kuozhan);
		$.ajax({
			url:"/typetemplate/updateInfo",
			data:{id:id,name:$("#name1").val(),brandListJson:brandListJson,specListJson:specListJson,kuozhanListJson:kuozhanListJson},
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 10000){
					typetemplateList();
				}
			},error:function(){
				alert("系统异常");
			}
		})
	}
	function deleteInfo(){
		var boxs = document.getElementsByName("box");
		var ids = [];
		for (var i= 0;i<boxs.length;i++){
			if (boxs[i].checked){
				ids[i] = boxs[i].value;
			}
		}
		if (ids.length == 0){
			alert("请选择要删除的选项");
			return;
		}
		$.ajax({
			url:"/typetemplate/deleteInfo",
			data:{"ids":ids},
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 10000){
					typetemplateList();
				}
			},error:function(){
				alert("系统异常");
			}
		})
	}
</script>
</body>

</html>